package upicgame.game.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import upicgame.entity.Game;
import upicgame.game.dao.GameDao;

import javax.annotation.Resource;
import java.util.List;

/*
 *Created by GHW
 *Description:game Service层实现
 *2019/12/17 11:19
 */
@Service
@Transactional
public class GameServiceImpl implements GameService {
    @Resource
    private GameDao gd;

    //添加游戏
    public boolean addGame(Game game) {
        //如果查询数据库没有此游戏，则允许添加
        if (gd.findGameByName(game.getName()) == null) {
            gd.addGame(game);
            System.out.println("添加游戏：[" + game.getName() + "]");
            return true;
        } else {
            System.out.println("库中已有此游戏，请检查后重新添加");
            return false;
        }
    }

    //ID单查游戏
    public Game findGameById(Integer id) {
        if (id != null) {
            return gd.findGameById(id);
        } else {
            System.out.println("id为空");
            return null;
        }
    }

    //name单查游戏
    public boolean findGameByName(String name) {
        //返回非空
        return gd.findGameByName(name) != null;
    }

    //查找所有游戏
    public List<Game> findAllGame() {
        return gd.findAllGame();
    }

    //修改游戏信息
    public void updateGame(Game game) {
        if (game != null) {
            gd.updateGameInfo(game);
        } else {
            System.out.println("game为空");
        }
    }

    //分页全查
    @Override
    public List<Game> findAllGameByPage(Integer pageNum, Integer pageSize) {
        return gd.findAllGameByPage(pageNum, pageSize);
    }

    //查找总条数
    @Override
    public int findCount() {
        return gd.findCount();
    }

    //删除游戏
    public void deleteGame(Integer id) {
        if (id != null) {
            Game game = gd.findGameById(id);
            gd.deleteGame(game);
        } else {
            System.out.println("game为空");
        }
    }

    //游戏搜索带分页，模糊搜索
    @Override
    public List<Game> findAllGameByPageWithFuzzy(Integer pageNum, Integer pageSize, String gameName) {
        return gd.findAllGameByPageWithFuzzy(pageNum, pageSize, gameName);
    }
}
